 import pandas as pd
import numpy as np

def explain_clinical_variables():
    """详细解释clinical_data.csv中各个变量的含义"""
    
    # 读取数据
    clinical_data = pd.read_csv('./preprocessed/clinical_data.csv')
    
    print("=== clinical_data.csv 变量详细解释 ===\n")
    
    # 1. Patient ID
    print("1. Patient ID (患者ID)")
    print("   - 含义: 患者的唯一标识符")
    print("   - 格式: C3N-XXXXX 或 C3L-XXXXX")
    print("   - 示例: C3N-00246, C3L-01352")
    print(f"   - 唯一患者数: {clinical_data['Patient ID'].nunique()}")
    print()
    
    # 2. Patient Sex
    print("2. Patient Sex (患者性别)")
    print("   - 含义: 患者的性别")
    print("   - 取值: M (男性), F (女性)")
    print("   - 分布:")
    sex_counts = clinical_data['Patient Sex'].value_counts()
    for sex, count in sex_counts.items():
        print(f"     {sex}: {count} 样本 ({count/len(clinical_data)*100:.1f}%)")
    print()
    
    # 3. Study Date
    print("3. Study Date (检查日期)")
    print("   - 含义: 医学影像检查的日期")
    print("   - 格式: YYYY-MM-DD HH:MM:SS")
    print("   - 示例: 2009-10-29 00:00:00.0")
    print(f"   - 日期范围: {clinical_data['Study Date'].min()} 到 {clinical_data['Study Date'].max()}")
    print()
    
    # 4. Study Description
    print("4. Study Description (检查描述)")
    print("   - 含义: 医学影像检查的详细描述")
    print("   - 常见类型:")
    study_desc_counts = clinical_data['Study Description'].value_counts().head(10)
    for desc, count in study_desc_counts.items():
        print(f"     {desc}: {count} 次")
    print()
    
    # 5. Patient Age
    print("5. Patient Age (患者年龄)")
    print("   - 含义: 患者检查时的年龄")
    print("   - 格式: XXXY (如044Y表示44岁)")
    print("   - 年龄分布:")
    age_values = clinical_data['Patient Age'].unique()
    age_counts = clinical_data['Patient Age'].value_counts().head(10)
    for age, count in age_counts.items():
        print(f"     {age}: {count} 样本")
    print()
    
    # 6. Modality
    print("6. Modality (影像模态)")
    print("   - 含义: 医学影像的成像方式")
    print("   - 常见类型:")
    modality_counts = clinical_data['Modality'].value_counts()
    for modality, count in modality_counts.items():
        print(f"     {modality}: {count} 样本 ({count/len(clinical_data)*100:.1f}%)")
    print()
    
    # 7. Series Date
    print("7. Series Date (序列日期)")
    print("   - 含义: 特定影像序列的采集日期")
    print("   - 通常与Study Date相同或相近")
    print()
    
    # 8. Series Description
    print("8. Series Description (序列描述)")
    print("   - 含义: 特定影像序列的详细描述")
    print("   - 包含扫描参数、重建方式等信息")
    print("   - 常见类型:")
    series_desc_counts = clinical_data['Series Description'].value_counts().head(10)
    for desc, count in series_desc_counts.items():
        print(f"     {desc}: {count} 次")
    print()
    
    # 9. Body Part Examined
    print("9. Body Part Examined (检查部位)")
    print("   - 含义: 影像检查的身体部位")
    print("   - 分布:")
    body_part_counts = clinical_data['Body Part Examined'].value_counts()
    for part, count in body_part_counts.items():
        print(f"     {part}: {count} 样本 ({count/len(clinical_data)*100:.1f}%)")
    print()
    
    # 10. Series Number
    print("10. Series Number (序列编号)")
    print("    - 含义: 在同一检查中的序列顺序编号")
    print("    - 范围:")
    print(f"      最小值: {clinical_data['Series Number'].min()}")
    print(f"      最大值: {clinical_data['Series Number'].max()}")
    print(f"      平均值: {clinical_data['Series Number'].mean():.1f}")
    print()
    
    # 11. Image Count
    print("11. Image Count (图像数量)")
    print("    - 含义: 该序列包含的影像数量")
    print("    - 统计信息:")
    print(f"      最小值: {clinical_data['Image Count'].min()}")
    print(f"      最大值: {clinical_data['Image Count'].max()}")
    print(f"      平均值: {clinical_data['Image Count'].mean():.1f}")
    print(f"      中位数: {clinical_data['Image Count'].median():.1f}")
    print()
    
    # 数据质量分析
    print("=== 数据质量分析 ===")
    print(f"总记录数: {len(clinical_data)}")
    print(f"唯一患者数: {clinical_data['Patient ID'].nunique()}")
    print(f"每个患者的平均记录数: {len(clinical_data)/clinical_data['Patient ID'].nunique():.1f}")
    
    # 缺失值分析
    print("\n缺失值分析:")
    missing_data = clinical_data.isnull().sum()
    for column, missing_count in missing_data.items():
        if missing_count > 0:
            print(f"  {column}: {missing_count} 缺失值 ({missing_count/len(clinical_data)*100:.1f}%)")
    
    print("\n=== 变量用途总结 ===")
    print("1. 标识变量: Patient ID (用于数据关联)")
    print("2. 人口统计学变量: Patient Sex, Patient Age")
    print("3. 时间变量: Study Date, Series Date")
    print("4. 临床变量: Study Description, Series Description")
    print("5. 技术变量: Modality, Body Part Examined, Series Number, Image Count")
    print("6. 这些变量可用于:")
    print("   - 患者特征分析")
    print("   - 检查类型分类")
    print("   - 时间序列分析")
    print("   - 数据质量控制")

if __name__ == '__main__':
    explain_clinical_variables()